import { mapGetters } from 'vuex'
import topoData from '../data'
export default {
  data() {
    return {
      nodes: [],
      edges: [],
      stationList: [],
      cableList: [],
      currentNode: {
        nodeId: '',
        type: '',
        label: ''
      },
      currentDevice: {
        nodeId: '',
        nodeType: '',
        nodeCategory: ''
      },
      currentEdge: {
        id: '',
        label: ''
      },
      gisHeight: 0, // 高德地图容器高度
      hoverPositionLeft: 0,
      hoverPositionTop: 0,
      hoverProp: false,
      fiberInfo: {},
      filterNodeId: '',
      stationInfo: {},
      formData: {}
    }
  },
  computed: {
    ...mapGetters(['regionTree',]),
    // 光缆详情绝对定位
    hoverPosition() {
      let top = null
      let bottom = null
      if (this.gisHeight - this.hoverPositionTop - 146 > 0) {
        top = this.hoverPositionTop + 'px'
      } else {
        bottom = '0px'
      }
      // 获取当前鼠标在画布中的位置
      return {
        left: this.hoverPositionLeft + 'px', // 鼠标校准后的位置 140
        top: top,
        bottom: bottom
      }
    }
  },
  watch: {
    filterNodeId: function(newVal, oldVal) {
      // 取消当前高亮
      oldVal && this.removeHightLightMarker(oldVal)
      // 设置选中高亮
      newVal && this.hightLightMarker(newVal)
    }
  },
  methods: {
    getTopos() {
      this.nodes = []
      this.edges = []
      this.stationList = []
      this.cableList = []
      return new Promise(resolve => {
        this.nodes = topoData.nodes
        this.edges = topoData.edges
        this.filterData(resolve)
      })
    },
    // 处理站点的经纬度，以及连线关系
    filterData(resolve) {
      this.nodes.forEach(i => {
        if (i.longitude && i.latitude) {
          this.stationList.push(i)
        }
      })
      this.edges.forEach(i => {
        const fromNode = this.stationList.find(j => i.fromNodeId === j.nodeId)
        if (fromNode && fromNode.longitude && fromNode.latitude) {
          i.fromLngLat = [fromNode.longitude, fromNode.latitude]
          i.fromNodeName = fromNode.label
        }
        const toNode = this.stationList.find(j => i.toNodeId === j.nodeId)
        if (toNode && toNode.longitude && toNode.latitude) {
          i.toLngLat = [toNode.longitude, toNode.latitude]
          i.toNodeName = toNode.regionName
        }
        if (i.fromLngLat && i.toLngLat) {
          i.centerPoint = this.getCenterPoint([
            { lng: i.fromLngLat[0], lat: i.fromLngLat[1] },
            { lng: i.toLngLat[0], lat: i.toLngLat[1] }
          ])
          this.cableList.push(i)
        }
      })
      resolve()
    },
    // geoCoordinateList = [
    //   {lng: 104.12433, lat: 30.608965}, // 起始点经纬度
    //   {lng: 104.165251, lat: 30.642022} // 结束点经纬度
    // ]
    // 根据两个点的经纬度，得出中心点的经纬度
    getCenterPoint(geoCoordinateList) {
      const total = geoCoordinateList.length
      let X = 0
      let Y = 0
      let Z = 0
      for (const g of geoCoordinateList) {
        const lat = g.lat * Math.PI / 180
        const lng = g.lng * Math.PI / 180
        const x = Math.cos(lat) * Math.cos(lng)
        const y = Math.cos(lat) * Math.sin(lng)
        const z = Math.sin(lat)
        X += x
        Y += y
        Z += z
      }
      X = X / total
      Y = Y / total
      Z = Z / total
      const lng = Math.atan2(Y, X)
      const Hyp = Math.sqrt(X * X + Y * Y)
      const Lat = Math.atan2(Z, Hyp)
      return { lng: lng * 180 / Math.PI, lat: Lat * 180 / Math.PI }
    },
    addMidlePort(arrPoints, offset = 0) {
      if (arrPoints.length === 2) {
        const middlePoint = [
          (arrPoints[0][0] + arrPoints[1][0]) / 2 + offset,
          (arrPoints[0][1] + arrPoints[1][1]) / 2 + offset
        ]
        arrPoints.splice(1, 0, middlePoint)
        console.log(JSON.parse(JSON.stringify(arrPoints)))
        // 开始和结束点相同的情况再添加两个点
        if (arrPoints[0][0] === arrPoints[2][0] && arrPoints[0][1] + arrPoints[2][1]) {
          const middlePoint2 = [
            middlePoint[0] - offset,
            middlePoint[1]
          ]
          arrPoints.splice(1, 1, middlePoint2)
          const middlePoint3 = [
            middlePoint[0],
            middlePoint[1] - offset
          ]
          arrPoints.splice(2, 0, middlePoint3)
        }
      }
      return arrPoints
    },
  }
}
